/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.syr.iis.web.model.DialogueActDimension;
import edu.syr.iis.web.model.DialogueActDimensionManager;
import edu.syr.iis.web.model.DialogueActLabel;
import edu.syr.iis.web.model.DialogueActLabelManager;
import edu.syr.iis.web.model.DialogueActSchemeManager;
import edu.syr.iis.web.util.Key;

/**
 * @author kinoue
 *
 */
public class EnterDialogueActLabelController extends SimpleFormController
{
    
    private DialogueActLabelManager dialogueActLabelManager = null;
    private DialogueActDimensionManager dialogueActDimensionManager = null;
    private DialogueActSchemeManager dialogueActSchemeManager = null;

    /**
     * Returns a new instance of DialogueActLabel object if "dialogueActLabelId" 
     * HTTP parameter is specified; other returns instance of object in database 
     * matching the value of the parameter.
     * @see DialogueActLabel
     */
    	
    @Override
	protected Object formBackingObject(HttpServletRequest request)
    {
	System.out.println("Entering EnterDialogueActLabelController!");
	
	
	
	DialogueActLabel dialogueActLabel = null;
        
	if (request.getParameter(Key.DA_LABEL_ID) != null
                && request.getParameter(Key.DA_LABEL_ID).trim().length() > 0) 
	{
            int id = Integer.parseInt(request.getParameter(Key.DA_LABEL_ID));
            dialogueActLabel = dialogueActLabelManager.getDialogueActLabel(id, false);
        } else 
        {
            dialogueActLabel = new DialogueActLabel();
        }

	System.out.println("Return converlationLabel with ID: " + dialogueActLabel.getDialogueActLabelId());
        return dialogueActLabel;
    }   

    @Override
	protected Map referenceData(HttpServletRequest request) throws Exception
    {
        HashMap model = new HashMap();
        model.put(Key.DA_LABELS, dialogueActLabelManager.getDialogueActLabels());
        model.put(Key.DA_DIMENSIONS, dialogueActDimensionManager.getDialogueActDimensions());
        model.put(Key.DA_SCHEMES, dialogueActSchemeManager.getDialogueActSchemes());

        return model;
    }

    @Override
	protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors)
    {
	DialogueActLabel dialogueActLabel = (DialogueActLabel) command;
	
	System.out.println("Entinering onSubmit with ");
	System.out.println(" id:    " + dialogueActLabel.getDialogueActLabelId());
	System.out.println(" label: " + dialogueActLabel.getLabel());
	System.out.println(" desc: " + dialogueActLabel.getDescription());
        
        dialogueActLabelManager.saveDialogueActLabel(dialogueActLabel);
        return new ModelAndView(getSuccessView());
    }

    public DialogueActLabelManager getDialogueActLabelManager()
    {
        return dialogueActLabelManager;
    }

    public void setDialogueActLabelManager(
    	DialogueActLabelManager dialogueActLabelManager)
    {
        this.dialogueActLabelManager = dialogueActLabelManager;
    }

    public DialogueActDimensionManager getDialogueActDimensionManager()
    {
        return dialogueActDimensionManager;
    }

    public void setDialogueActDimensionManager(
    	DialogueActDimensionManager dialogueActDimensionManager)
    {
        this.dialogueActDimensionManager = dialogueActDimensionManager;
    }
    
    public void setDialogueActSchemeManager(
    	DialogueActSchemeManager dialogueActSchemeManager)
    {
        this.dialogueActSchemeManager = dialogueActSchemeManager;
    }

    @Override
	protected void initBinder(
	    HttpServletRequest request,
	    ServletRequestDataBinder binder) throws Exception {
    	System.out.println("Entering EnterDialogueActLabelController:initBinder...");
	binder.registerCustomEditor(DialogueActDimension.class, new DialogueActDimensionPropertyEditor());

    }
    
    class DialogueActDimensionPropertyEditor extends PropertyEditorSupport
    {
	@Override
	public void setAsText(String text)  throws IllegalArgumentException 
	{
	    int id = Integer.parseInt(text);
	    DialogueActDimension daDimension = dialogueActDimensionManager.getDialogueActDimension(id);

	    if (daDimension == null) {
		throw new IllegalArgumentException("No Dialogue Act Dimension found with id: " + id);
	    }
	    setValue(daDimension);
	}

    }

}
